package com.zp.qualitypractice.Dialog;

import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zp.qualitypractice.R;

/**
 * Created by izp on 2017/6/15.
 */

public class SimpleDialogFragment extends DialogFragment {
    private View mRootView;
    private BeforeShow mBeforeShow;
    private AfterShow mAfterShow;

    public SimpleDialogFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (null==mRootView ){
            mRootView = inflater.inflate(R.layout.dialog_text, container, false);
        }
        return mRootView;
    }

    public void setStatus(String status){
        if (null==mRootView)return;
        TextView textView = (TextView) mRootView.findViewById(R.id.dialogText);
        if (null==textView)return;
        textView.setText(status);
    }


    public int show(FragmentManager manager, String tag,int transit) {
        if (null!=mBeforeShow){
            mBeforeShow.doBefore();
        }
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.setTransition(transit);
//        transaction.addToBackStack(null);
        if (isAdded()){
            return transaction.show(this).commit();
        }else {
            return show(transaction,tag);
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

//        if (null!=mRootView&&isVisible()&&null!=mAfterShow){
//            KLog.v("运行没？");
//            mAfterShow.doAfter();
//        }
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {

        if (null!=mRootView&&isVisibleToUser&&null!=mAfterShow){

            mAfterShow.doAfter();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onResume() {

//        if (null!=mRootView&&isVisible()&&null!=mAfterShow){
//            KLog.v("运行没？");
//            mAfterShow.doAfter();
//        }
        super.onResume();

    }

    public SimpleDialogFragment setBeforeEvent(BeforeShow beforeEvent){
        this.mBeforeShow = beforeEvent;
        return this;
    }
    public SimpleDialogFragment setAfterEvent(AfterShow afterEvent){
        this.mAfterShow = afterEvent;
        return this;
    }

    public interface BeforeShow{
        void doBefore();
    }
    public interface AfterShow{
        void doAfter();
    }

}
